package leetcode;

import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String temp = tokens[i];
            if((temp.equals("+"))||(temp.equals("-"))||(temp.equals("*"))||(temp.equals("/"))){
               if(temp.equals("+")){
                    stack.push(stack.pop() + stack.pop());
                } else if (temp.equals("*")) {
                    stack.push(stack.pop() * stack.pop());
                } else if (temp.equals("-")) {
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num2 - num1);
                } else if (temp.equals("/")) {
                    int num1 = stack.pop();
                    int num2 = stack.pop();
                    stack.push(num2 / num1);
                }
            }
            else {
                stack.push(Integer.parseInt(temp));
            }
        }
        return stack.pop();
    }
    }


